package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Trader;
import com.sls.web.manager.pojo.UserInfoVO;

public class TraderClient {
	private static final Logger log = LoggerFactory.getLogger(TraderClient.class) ;
	private static Lock lock = new ReentrantLock() ;
	private static volatile TraderClient instance = null ;
	private TraderClient(){
		
	}
	public static TraderClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if (instance == null) {
					instance = new TraderClient();
				}
			} finally{
				lock.unlock() ;
			}
		}
		return instance ;
	}
	public Trader getTrader(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300008") ;
		head.setRequestId(UUID.randomUUID().toString());
		
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, Trader.class) ;
		}
		return null ;
	}
	public Trader getTraderByMobile(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300027") ;
		head.setRequestId(UUID.randomUUID().toString());
		
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, Trader.class) ;
		}
		return null ;
	}
	public List<Trader> getTraderList(UserInfoVO userInfoVO , Trader trader, int pageNo , int pageSize){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300021") ;
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("trader", trader) ;
		paramMap.put("pageNo", pageNo) ;
		paramMap.put("pageSize", pageSize) ;
		SocketDTO socketDto = SocketUtil.send(head, paramMap) ;
		if(ResultCode.success.getCode().equals(socketDto.getHead().getResponseCode())){
			String body = socketDto.getBody() ;
			Gson gson = new Gson() ;
			return gson.fromJson(body, new TypeToken<List<Trader>>(){}.getType()) ;
		}
		return null ;
	}
	public int getTraderCount(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300023");
		head.setRequestId(UUID.randomUUID().toString());
		
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("trader", trader) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Integer>(){}.getType()) ;
			}
		}
		return 0 ;
	}
	public boolean checkTrader(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300034");
		head.setRequestId(UUID.randomUUID().toString());
		trader = trader.checkTrader(userInfoVO) ;
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
			}
		}
		return false ;
	}
	public boolean updateTraderStatus(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300047");
		head.setRequestId(UUID.randomUUID().toString());
		trader = trader.checkTrader(userInfoVO) ;
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
			}
		}
		return false ;
	}
	public Trader getTraderByName(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300036") ;
		head.setRequestId(UUID.randomUUID().toString());
		
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<Trader>(){}.getType()) ;
		}
		return null ;
	}
}
